<?php
namespace app\index\controller;

use think\Db;
use think\Session;
use app\common\controller\IndexBase;

/**
* 
*/
class Order extends IndexBase
{
	
	public function order(){
        if( request()->isPost() ){
            $userinfo = Session::get('userInfo');
            $map = input('post.');
            $data = Db::name('goods')->where('id',$map['goods_id'])->find();
            //$address = Db::name("address")->where('user_id',$this->userInfo["id"])->select();
            // foreach($address as $k=>$v){
            //     $address[$k]["details"] = str_replace(">"," ",$v["details"]);
            // }
            $is_shop = 0;
            if($userinfo){
                $is_shop = get_user_is_shop($userinfo['id']);
            }
            if($is_shop == 0 && $data['type'] == 1){
                $data['price'] = $data['first_price'];
            }elseif($data['type'] == 2){
                $data['price'] = $data['cz_price'];
            }elseif($data['type'] == 3){
                $data['price'] = $data['dl_price'];
            }

            $this->titleName = "订单确认";
            $data["buy_num"] = $map["goods_num"];
            //$this->assign("address",$address);
            $this->assign('data',$data);
            //print_r($data);
            return $this->fetch();
        }else{
            $this->error("订单错误");
        }
    }

    public function pay(){
        $userinfo = Session::get('userInfo');
        if( request()->isAjax() ) {
            $data = input('post.');
            if( !isset($data["goods_id"]) || empty($data["goods_id"]) || !isset($data["buy_num"]) || empty($data["buy_num"]) ){
                $retArr = [
                    "id" => 1,
                    "msg" => "你的数据有误"
                ];
                $this->json_return($retArr);
            }
            $goods = Db::name("Goods")->field("id,name,type,price,first_price,cz_price,dl_price,images")->where(array("id"=>$data["goods_id"],"status"=>1))->find();
            if( empty($goods) ){
                $retArr = [
                    "id" => 1,
                    "msg" => "你购买的商品不存在"
                ];
                $this->json_return($retArr);
            }
            $is_shop = 0;
            if($userinfo){
                $is_shop = get_user_is_shop($userinfo['id']);
            }
            if($is_shop == 0 && $goods['type'] == 1){
                $goods['price'] = $goods['first_price'];
            }elseif($goods['type'] == 2){
                $goods['price'] = $goods['cz_price'];
            }elseif($goods['type'] == 3){
                $goods['price'] = $goods['dl_price'];
            }
            
            $userInfo = Db::name("user_info")->where("user_id",$this->userInfo["id"])->find();
            $total_price = $goods["price"] * $data["buy_num"];

            if( $data["pay_type"] == "WeChat" ){
                $pay_type = 1;
                $pay_name = '微信支付';
            }elseif( $data["pay_type"] == "Alipay" ){
                $pay_type = 2;
                $pay_name = '支付宝';
            }else{
                $pay_type = 3;
                $pay_name = '线下支付';
            }
            $Arr = array(
                'order_sn'       => generateOrderCode("gw",$this->userInfo["id"]),
                'user_id'        => $this->userInfo["id"],
                'status'         => 1,
                'goods_id'       => $goods['id'],
                'goods_name'     => $goods['name'],
                'goods_image'    => $goods['images'],
                'goods_price'    => $goods['price'],
                'num'            => $data['buy_num'],
                'amount'         => $goods['price']*$data['buy_num'],
                'pay_id'         => $pay_type,
                'pay_name'       => $pay_name,
                'add_time'       => time(),
                'remark'         => $data['remark']
                );
            $id = Db::name("Order")->insertGetId($Arr,false,true);
            if( $id ){
                if( $data["pay_type"] == "WeChat" ){
                    $jump_url = request()->domain()."/index/Wxpay/pay?id=gw_".$id;
                }elseif($data["pay_type"] == "Alipay"){
                    $jump_url = "/index.php/Home/Alipay/pay?id=".$id;
                }else{
                    $jump_url = "/index/order/proof/id/$id";
                }

                $retArr = [
                    "id" => 0,
                    "msg" => '',
                    "content" => array(
                        "url" => $jump_url
                    )
                ];
            }else{
                $retArr = [
                    "id" => 1,
                    "msg" => '订单创建失败'
                ];
            }
            $this->json_return($retArr);
        }
    }

    //支付凭证
    public function proof(){

        $map = input("param.");
        if( empty($map["id"])){
            $this->error("错误数据");
        }
        $price = Db::name("Order")->where("id",$map["id"])->value("amount");
        //收款人信息
        $Arr = Db::name("Config")->where(array("extend_value"=>"income"))->field("key,value")->select();
        
        $Arr = return_key_val($Arr,"key","value");
        $Arr["price"] = $price;
        $Arr["id"] = $map["id"];
        //print_r($Arr);exit;
        $this->titleName = "线下支付";
        $this->assign("Arr",$Arr);
        return $this->fetch();
    }
    //支付凭证
    public function up_proof(){

        if( request()->isPost() ){
            $id = input("post.id");
            $file = request()->file('file');
            if($file){
                $info = $file->move(ROOT_PATH . '/public' . DS . 'Uploader/Package/proof');
                if($info){
                    $imgUrl = '/Uploader/Package/proof/'.$info->getSaveName();
                    Db::name("Order")->where("id",$id)->update(array("proof"=>$imgUrl,"status"=>5,'pay_time'=>time()));
                    $this->redirect('order/order_list');
                }else{
                    $this->error("上传支付凭证失败");
                }
            }
        }
    }

    /*public function verify_pay(){
        if( request()->isAjax() ){
            $data = input('post.');
            if( !isset($data["price"]) || empty($data["price"]) || !isset($data["buy_sum"]) || empty($data["buy_sum"])  ){
                $retArr = [
                    "id" => 1,
                    "msg" => "你的数据有误"
                ];
                $this->json_return($retArr);
            }
            $userInfo = Db::name("userinfo")->field("cash,integral")->where("user_id",$this->userInfo["id"])->find();
            $tatol = $data["price"] * $data["buy_sum"];
            if( $tatol > $userInfo["cash"] + $userInfo["integral"] ){
                $retArr = [
                    "id" => 1,
                    "msg" => "对不起你的积分不足"
                ];
            }else{
                if( $tatol > $userInfo["integral"] ){
                    if( empty($userInfo["integral"]) ||  $userInfo["integral"] ==  '0.00'){
                        $str = "需要提现币<span style='color: #007AFF;'>".$tatol."</span>";
                    }else{
                        $str = "需要购物积分<span style='color: red;'>".$userInfo["integral"]."</span>提现币<span style='color: #007AFF;'>".($tatol-$userInfo["integral"])."</span>";
                    }
                }else{
                    $str = "需要购物积分<span style='color: red;'>".$userInfo["integral"]."</span>";
                }
                $retArr = [
                    "id" => 0,
                    "msg" => $str
                ];
            }
            $this->json_return($retArr);
        }
    }*/

    //购买商品记录
    public function order_list(){

        $list = Db::name("Order")->where(array("status"=>["gt",0],"user_id"=>$this->userInfo["id"]))->order('add_time desc')->select();

        $this->titleName = "我的订单";
        $this->assign("data",$list);
        return $this->fetch();
    }

    public function detail(){
        $order_id = input('param.id');
        $order = Db::name('Order')->where('id',$order_id)->find();
        $this->assign("order",$order);
        
        return $this->fetch();
    }
}